package juan.happyge.plugin.myi.service.book.crawler;

import juan.happyge.plugin.myi.config.BookConfig;
import juan.happyge.plugin.myi.config.ConfigManage;
import juan.happyge.plugin.myi.pojo.crawler.CrawlerChapterDetailPojo;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 小说爬虫
 * 目前只支持爬取笔趣阁网站的小说
 * 笔趣阁搜索链接 https://so.biqusoso.com/s.php?ie=utf-8&siteid=qu-la.com&q=临高启明
 */
public class BookCrawlerBQG extends AbstractBookCrawler{

    /**
     * 分析html界面
     * @param document
     * @throws Exception
     */
    @Override
    public CrawlerChapterDetailPojo analyze(Document document){
        if(document == null){return null;}

        String titleDat = document.title().split("_第")[1];
        String title = "第" +  titleDat.split("_笔趣阁")[0].split("-笔趣阁")[0];
        System.out.println(title);

        Element contentEle = document.getElementById("content");
        if(contentEle == null){
            System.out.println("爬取完成");
            return null;
        }

        String content = contentEle.text();
        content = content.replace("。","。\n\n");

        String nextUrl = "";
        Elements links = document.select("a[href]");
        for (Element link : links) {
            if("下一章".equals(link.text())){
                String href = link.attr("href");
                if(href.contains(".html")){
                    nextUrl = "http://www.ibiquge.com" + href;
                }
                System.out.println(nextUrl);
            }
        }

        CrawlerChapterDetailPojo crawlerChapterDetailPojo = new CrawlerChapterDetailPojo();
        crawlerChapterDetailPojo.setTitle(title);
        crawlerChapterDetailPojo.setContent(content);
        crawlerChapterDetailPojo.setNextUrl(nextUrl);
        return crawlerChapterDetailPojo;
    }


}
